<?php

function getTradeHTML($loggedInTeam, $allTradesPage, $myTradesPage, $maxPerPage) {
        $league = 4;
        if ($loggedInTeam != null) {
           $league = $loggedInTeam->leagueId;
        }
	$teams = getTeams($league, false);
	$allTrades = getTradesByLeague($league, $allTradesPage, $maxPerPage);
	$allLtiSpuds = getLtisSpudByLeague($league);
	$numTrades = getCountTradesByLeague($league);
	$numLtiSpuds = getCountLtiSpudsByLeague($league);
	$count3 = count($allTrades);
	$count4 = $numTrades/$maxPerPage;
	if ($numTrades%$maxPerPage > 0) {
		$count4++;
	}
	$count5 = count($allLtiSpuds);
	$count6 = $numLtiSpuds/$maxPerPage;
	if ($numLtiSpuds%$maxPerPage > 0) {
		$count6++;
	}
        if ($loggedInTeam != null) {
        $result = "<b>Trading between coaches is disabled during the season.</b>";
	//$result = "<form action='../trades/setUpTrade.php' method='post' target='hiddenFrame'>";
	//$result .= "<table align='center' width='30%'>";
	//$result .= "<tr>";
	//$result .= "<td>Propose trade to:</td>";
	//$result .= "<td>";
	//$result .= "<select name='partyTwo'>";
	//$count = count($teams);
	//$result .= "<option>Select a coach</option>";
	//for ($i = 0; $i < $count; $i++) {
	//	if ($teams[$i]->coachName != $loggedInTeam->coachName) {
	//		$result .= "<option value='" . $teams[$i]->id . "'>" . $teams[$i]->coachName . "</option>";
	//	}
	//}
	//$result .= "</select>";
	//$result .= "</td>";
	//$result .= "<td><input type='button' value='Create Trade' onclick='submitCreateTradeForm(this.form)' class='buttonStyle'></td>";
	//$result .= "<tr>";
	//$result .= "</table>";
	//$result .= "</form><br><br>";
        }
	$result .= "<table align='center' width='100%'><tr><td>";
		$result .= "<b>Pages:</b>&nbsp;";
	for ($k = 1; $k <= $count4; $k++) {
		if ($k > 1) {
			$result .=  " | ";
		}
		$result .= "<a href='javascript: document.pageNumberForm.allTradesPage.value=$k;submitPageNumberFormFromTrades()'>$k</a>";
	}
	$result .= "</td</tr><tr><td valign='top'><table border='1'><tr><td valign='top'><h2>All Trades</h2></td></tr>";
	$count = count($allTrades);
	for ($i = 0; $i < $count; $i++) {
		$result .= "<tr><td>" . getTradeString($allTrades[$i]) . "</td></tr>";
	}
	$result .= "</table></td><td valign='top'><table border='1'><tr><td valign='top'><h2>All Lti/Spuds</h2></td></tr>";
	
	$count = count($allLtiSpuds);
	for ($i = 0; $i < $count; $i++) {
		$result .= "<tr><td>" . getLtiString($allLtiSpuds[$i]) . "</td></tr>";
	}
	$result .= "</table></td>";
	
        if ($loggedInTeam != null) {
           $result .= "<td valign='top'>" . getMyTrades($loggedInTeam, $myTradesPage, $maxPerPage, true) . "</td></tr></table>";
        }
        else {
           $result .= "<td valign='top'></td></tr></table>";
        }
	
	return $result;
}

function getTradeHTMLForForum($leagueId) {
	$teams = getTeams($leagueId, false);
	$allTrades = getTradesByLeague($leagueId, -1, -1);
	$allLtiSpuds = getLtisSpudByLeague($leagueId);
	
	$result = "<table align='center' width='100%'><tr><td><table border='1'><tr><td valign='top'><b>All Trades</b></td></tr>";
	$count = count($allTrades);
	for ($i = 0; $i < $count; $i++) {
		$result .= "<tr><td>" . getTradeString($allTrades[$i]) . "</td></tr>";
	}
	$result .= "</table></td><td valign='top'><table border='1'><tr><td valign='top'><h2>All Lti/Spuds</h2></td></tr>";
	$count = count($allLtiSpuds);
	for ($i = 0; $i < $count; $i++) {
		$result .= "<tr><td>" . getLtiString($allLtiSpuds[$i]) . "</td></tr>";
	}
	$result .= "</table></td></tr></table>";
	
	return $result;
}

function getMyTrades($loggedInTeam, $myTradesPage, $maxPerPage, $fromTradesPage) {
	$myTrades = getTradesByTeam($loggedInTeam->id, $myTradesPage, $maxPerPage);
	$numTrades = getCountTradesByTeam($loggedInTeam->id);
	$count3 = count($myTrades);
	$count4 = $numTrades/$maxPerPage;
	if ($numTrades%$maxPerPage > 0) {
		$count4++;
	}
	$result = "<b>Pages:</b>&nbsp;";
	for ($k = 1; $k <= $count4; $k++) {
		if ($k > 1) {
			$result .=  " | ";
		}
		$result .= "<a href='javascript: document.pageNumberForm.myTradesPage.value=$k;submitPageNumberForm";
		if ($fromTradesPage) {
			$result .= "FromTrades()";
		}
		else {
			$result .= "FromHome()";			
		}
		$result .= ";'>$k</a>";
	}
	$result .= "<table border='1'><tr><td valign='top' colspan='2'><h2>Your Trades</h2></td></tr>";
	$count = count($myTrades);
	for ($i = 0; $i < $count; $i++) {
		$result .= "<tr><td>" . getTradeString($myTrades[$i]) . "</td><td>" . displayState($myTrades[$i]->state) . isTradeProposedToMe($loggedInTeam, $myTrades[$i]) ."</td></tr>";
	}
	$result .= "</table>";
	
	return $result;
}

function isTradeProposedToMe($me, $trade) {
	if ($me->id == $trade->partyTwoId && $trade->state == TradeState::Proposed) {
		return "&nbsp;<input type='button' value='Confirm' onclick='confirmTrade($trade->id)'>&nbsp;<input type='button' value='Counter' onclick='counterTrade($trade->id)'>&nbsp;<input type='button' value='Reject' onclick='rejectTrade($trade->id)'>";
	}
}

function getTradeString($trade) {
	$result = "<table><tr><td><b>" . $trade->partyTwo . "</b> receives: ";
	$count = count($trade->partyTwoReceives);
	for ($i = 0; $i < $count; $i++) {
		$tradeItem = $trade->partyTwoReceives[$i];
		if ($tradeItem->itemType == TradeItemType::Player) {
			$result .= getPlayerDisplayForPlayer($tradeItem->player); 
		}
		else {
			$result .= "Pick #" . $tradeItem->pickNumber;
		}
		if ($i + 1 < $count) {
			$result .= ", ";
		}
	}
	$result .= "</td></tr><tr><td><b>" . $trade->partyOne . "</b> receives: ";
	$count = count($trade->partyOneReceives);
	for ($i = 0; $i < $count; $i++) {
		$tradeItem = $trade->partyOneReceives[$i];
		if ($tradeItem->itemType == TradeItemType::Player) {
			$result .= getPlayerDisplayForPlayer($tradeItem->player); 
		}
		else {
			$result .= "Pick #" . $tradeItem->pickNumber;
		}
		if ($i + 1 < $count) {
			$result .= ", ";
		}
	}
	$result .= "</td></tr></table>";

	return $result;	
}

function getTradeStringForForum($trade) {
	$result = "[table2][r][c][b]" . $trade->partyTwo . "[/b] receives [/c][c]";
	$count = count($trade->partyTwoReceives);
	for ($i = 0; $i < $count; $i++) {
		$tradeItem = $trade->partyTwoReceives[$i];
		if ($tradeItem->itemType == TradeItemType::Player) {
			$result .= getPlayerDisplayForForum($tradeItem->player); 
		}
		else {
			$result .= "Pick #" . $tradeItem->pickNumber;
		}
		if ($i + 1 < $count) {
			$result .= ", ";
		}
	}
	$result .= "[/c][/r][r][c][b]" . $trade->partyOne . "[/b] receives [/c][c]";
	$count = count($trade->partyOneReceives);
	for ($i = 0; $i < $count; $i++) {
		$tradeItem = $trade->partyOneReceives[$i];
		if ($tradeItem->itemType == TradeItemType::Player) {
			$result .= getPlayerDisplayForForum($tradeItem->player); 
		}
		else {
			$result .= "Pick #" . $tradeItem->pickNumber;
		}
		if ($i + 1 < $count) {
			$result .= ", ";
		}
	}
	$result .= "[/c][/r][/table2]";

	return $result;	
}

function validateProposedTrades($leagueId) {
	$proposedTrades = getProposedTrades($leagueId);
	$count = count($proposedTrades);
	for ($i = 0; $i < $count; $i++) {
		$count2 = count($proposedTrades[$i]->partyTwoReceives);
		for ($j = 0; $j < $count2; $j++) {
			$tradeItem = $proposedTrades[$i]->partyTwoReceives[$j];
			if ($tradeItem->itemType == TradeItemType::Player) {
				if (!isTeamPlayerValid($proposedTrades[$i]->partyOneId, $tradeItem->player->id)) {
					invalidateTrade($proposedTrades[$i]->id);
				}
			}
			else {
				if (!isPickValid($tradeItem->pickNumber, $proposedTrades[$i]->partyOneId)) {
					invalidateTrade($proposedTrades[$i]->id);
				}
			}
		}
		$count2 = count($proposedTrades[$i]->partyOneReceives);
		for ($j = 0; $j < $count2; $j++) {
			$tradeItem = $proposedTrades[$i]->partyOneReceives[$j];
			if ($tradeItem->itemType == TradeItemType::Player) {
				if (!isTeamPlayerValid($proposedTrades[$i]->partyTwoId, $tradeItem->player->id)) {
					invalidateTrade($proposedTrades[$i]->id);
				}
			}
			else {
				if (!isPickValid($tradeItem->pickNumber, $proposedTrades[$i]->partyTwoId)) {
					invalidateTrade($proposedTrades[$i]->id);
				}
			}
		}		
	}
}

function getCounterHTML($tradeId) {
	$trade = getTrade($tradeId);
	
	$partyOneTeam = getTeamById($trade->partyOneId, false);
	$partyTwoTeam = getTeamById($trade->partyTwoId, false);
	
	$partyTwoPicks = getPicks($trade->partyTwoId, false, $partyOneTeam->leagueId);
	$partyTwoPlayers = getDraftedPlayers($trade->partyTwoId);
	
	$partyOnePicks = getPicks($trade->partyOneId, false, $partyOneTeam->leagueId);
	$partyOnePlayers = getDraftedPlayers($trade->partyOneId);
	
	$result = "<form action='../trades/proposeCounter.php' target='hiddenFrame'><input type='hidden' name='parentTradeId' value='$tradeId'><table width='30%' align='center'>";
	$result .= "<tr><td width='40%'><table><tr><td><b>" . $partyOneTeam->coachName . "</b></td></tr><tr><td><b>Players</b></td></tr>";
	$result .= "<tr><td><select id='partyOnePlayers' size='10' multiple>";
	$count = count($partyOnePlayers);
	$count2 = count($trade->partyTwoReceives);
	for ($i = 0; $i < $count; $i++) {
	        $foundIt = false;
	        for ($k = 0; $k < $count2; $k++) {
	           if ($trade->partyTwoReceives[$k]->itemType == TradeItemType::Player && $trade->partyTwoReceives[$k]->player->id == $partyOnePlayers[$i]->player->id) {
	              $foundIt = true;
	              break;
	           }
	        }
	        if (!$foundIt) { 
		   $result .= "<option value='" . $partyOnePlayers[$i]->player->id . "'>" . $partyOnePlayers[$i]->player->getDescription() . "</option>";  
	        }
	}
	$result .= getSpacerOption() . "</select></td></tr><tr><td><b>Picks</b></td></tr><tr><td><select id='partyOnePicks' size='10' multiple style='width: 80'>";
	$count = count($partyOnePicks);
	for ($i = 0; $i < $count; $i++) {
	        $foundIt = false;
	        for ($k = 0; $k < $count2; $k++) {
	           if ($trade->partyTwoReceives[$k]->itemType == TradeItemType::Pick && $trade->partyTwoReceives[$k]->pickNumber == $partyOnePicks[$i]->pickNumber) {
	              $foundIt = true;
	              break;
	           }
	        }
	        if (!$foundIt) { 
	 	   $result .= "<option value='" . $partyOnePicks[$i]->pickNumber . "'>#" . $partyOnePicks[$i]->pickNumber . "</option>";  
	        }
	}
	$result .= getSpacerOption() . "</select></td></tr><tr><td><b>Receives</b></td></tr><tr><td><select id='partyOneReceives' name='partyOneReceives[]' size='10' multiple>";
	$count = count($trade->partyOneReceives);
	for ($i = 0; $i < $count; $i++) {
	   if ($trade->partyOneReceives[$i]->itemType == TradeItemType::Pick) {
	      $result .= "<option value='#" . $trade->partyOneReceives[$i]->pickNumber . "'>#" . $trade->partyOneReceives[$i]->pickNumber . "</option>";  
	   }
	   else {
	      $result .= "<option value='" . $trade->partyOneReceives[$i]->player->id . "'>" . $trade->partyOneReceives[$i]->player->getDescription() . "</option>";  
	   }
	}
	$result .= getSpacerOption() . "</select></td></tr></table></td>";
	
	$result .= "<td width='20%'><table><tr><td align='center'><br><br><br><br><br><br><br><input type='button' value='Trade to -->' onclick='tradeToCounter(this.form)'></td></tr><tr><td><br><br></td></tr><tr><td align='center'><input type='button' value='<-- Trade for' onclick='tradeForCounter(this.form)'></td></tr><tr><td align='center'><input type='button' value='Remove Trade to' onclick='removeTradeTo(this.form)'></td></tr><tr><td align='center'><input type='button' value='Remove Trade for' onclick='removeTradeFor(this.form)'></td></tr></table></td>";
	
	$result .= "<td width='40%'><table><tr><td><b>" . $partyTwoTeam->coachName . "</b></td></tr><tr><td><b>Players</b></td></tr>";
	$result .= "<tr><td><select id='partyTwoPlayers' size='10' multiple>";
	$count = count($partyTwoPlayers);
	$count2 = count($trade->partyOneReceives);
	for ($i = 0; $i < $count; $i++) {
	        $foundIt = false;
	        for ($k = 0; $k < $count2; $k++) {
	           if ($trade->partyOneReceives[$k]->itemType == TradeItemType::Player && $trade->partyOneReceives[$k]->player->id == $partyTwoPlayers[$i]->player->id) {
	              $foundIt = true;
	              break;
	           }
	        }
	        if (!$foundIt) {
		   $result .= "<option value='" . $partyTwoPlayers[$i]->player->id . "'>" . $partyTwoPlayers[$i]->player->getDescription() . "</option>";  
	        }
	}
	$result .= getSpacerOption() . "</select></td></tr><tr><td><b>Picks</b></td></tr><tr><td><select id='partyTwoPicks' size='10' multiple>";
	$count = count($partyTwoPicks);
	for ($i = 0; $i < $count; $i++) {
	        $foundIt = false;
	        for ($k = 0; $k < $count2; $k++) {
	           if ($trade->partyOneReceives[$k]->itemType == TradeItemType::Pick && $trade->partyOneReceives[$k]->pickNumber == $partyTwoPicks[$i]->pickNumber) {
	              $foundIt = true;
	              break;
	           }
	        }
	        if (!$foundIt) {
		   $result .= "<option value='" . $partyTwoPicks[$i]->pickNumber . "'>#" . $partyTwoPicks[$i]->pickNumber . "</option>";  
	        }
	}
	$result .= getSpacerOption() . "</select></td></tr><tr><td><b>Receives</b></td></tr><tr><td><select id='partyTwoReceives' name='partyTwoReceives[]' size='10' multiple>";
	$count = count($trade->partyTwoReceives);
	for ($i = 0; $i < $count; $i++) {
	   if ($trade->partyTwoReceives[$i]->itemType == TradeItemType::Pick) {
	      $result .= "<option value='#" . $trade->partyTwoReceives[$i]->pickNumber . "'>#" . $trade->partyTwoReceives[$i]->pickNumber . "</option>";  
	   }
	   else {
	      $result .= "<option value='" . $trade->partyTwoReceives[$i]->player->id . "'>" . $trade->partyTwoReceives[$i]->player->getDescription() . "</option>";  
	   }
	}
	$result .= getSpacerOption() . "</select></td></tr></table></td></tr>";
	$result .= "<tr><td colspan='2'><input type='button' value='Clear' onClick='clearTrade()'>&nbsp;<input type='button' value='Cancel Counter' onClick='cancelTrade()'>&nbsp;<input type='button' value='Propose Counter' onClick='validateCounter(this.form)'></td></tr></table></form><form id='cancelForm' name='cancelForm' action='../trades/cancelTrade.php' method='post' target='hiddenFrame'></form>";
	
	return $result;
}

function getSpacerOption() {
	return "<option value=''>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" .
			"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" .
			"&nbsp;&nbsp;&nbsp;</option>";
}

?>
